/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package communication;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import model.Vaccinecontrol;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import model.Control;
/**
 * 
 * @author Fernando
 */
public class QVaccinecontrol extends RequestServer {

    private String table_name = "vaccinecontrol";
    private String json = "";
    private JSONArray array;
    private ArrayList<JSONObject> objList = new ArrayList<JSONObject>();
    private Integer idVaccinecontrol = null;
    private String dose = null;
    private Integer Control_idControl;    

    public static void main(String[] args) throws IOException, JSONException {
        //USAGE
        ///*GET ALL REGISTERS
        QVaccinecontrol SV = new QVaccinecontrol();
        
        ArrayList<Vaccinecontrol> All = SV.GetAll();
        
        for (Vaccinecontrol V : All){
        
        System.out.println(V.getDose());        
        }
        //*/
        /*GET ONE REGISTER
        QVaccinecontrol SV = new QVaccinecontrol(2);
        //OR: QVaccine SV = new QVaccine(name); 
        
        Vaccinecontrol V = SV.GetObject(0); // index 0, the first returned value
        System.out.println(V.getDose());
        //*/
        /*DELETE
        QVaccinecontrol QV = new QVaccinecontrol();
        
        String R = QV.Delete(null, null);
        //*/
        /*UPDATE
        QVaccinecontrol QV = new QVaccinecontrol();
        
        String R = QV.Update(2, "noasdadvonome");
        //*/
        /*INSERT 
        QVaccinecontrol QV = new QVaccinecontrol();
        
        Control c = new Control(7);
        
        String answer = QV.Insert("120mg55", c);
        //*/
    }

    public JSONObject Serialize(Vaccinecontrol V) throws JSONException {

        JSONSerializer serializer = new JSONSerializer();
        
        this.json = serializer.serialize(V);
        
        JSONObject obj = new JSONObject(this.json);

        obj.remove("class");
        return obj;
    }

    public String Insert(String dose, Control control) throws UnsupportedEncodingException, IOException, JSONException {
        
        Vaccinecontrol V = new Vaccinecontrol(dose, control.getIdControl());
        
        JSONObject obj = this.Serialize(V);

        obj.remove("idVaccineControl");

        String post = this.post("[" + obj.toString() + "]", this.table_name);

        return post;
    }

    public String Update(int id, String dose) throws UnsupportedEncodingException, IOException, JSONException {

        Vaccinecontrol V = new Vaccinecontrol(id, dose);
        this.idVaccinecontrol = id;

        JSONObject obj = this.Serialize(V);
        
        obj.remove("control_idControl");

        this.json = obj.toString();

        String parameters = PrepareParameters();

        System.out.println(this.json);
        System.out.println(parameters);

        String put = this.put(this.table_name, this.json, parameters);

        return put;
    }

    public String Delete(Integer id, String name) throws IOException {

        this.idVaccinecontrol = id;
        this.dose = name;

        String delete = this.delete(this.table_name, PrepareParameters());

        return delete;
    }

    private boolean SetJsonList() throws JSONException {

        if (this.json == "") {
            return false;
        }

        this.array = new JSONArray(this.json);

        for (int i = 0; i < array.length(); i++) {

            JSONObject tmp = array.getJSONObject(i);
            objList.add(tmp);
        }
        return true;
    }

    public Vaccinecontrol GetObject(int index) throws JSONException {

        if (this.json == "") {
            Vaccinecontrol V = new Vaccinecontrol();
            return V;
        }
        this.array = new JSONArray(this.json);

        JSONObject obj = null;

        obj = objList.get(index);

        obj.put("class", Vaccinecontrol.class.getCanonicalName());

        String djson = obj.toString();

        Vaccinecontrol V = new JSONDeserializer<Vaccinecontrol>().deserialize(djson);

        return V;
    }

    public ArrayList<Vaccinecontrol> GetAll() throws JSONException, IOException {

        this.json = this.get(this.table_name, null);
        SetJsonList();

        ArrayList<Vaccinecontrol> Varray = new ArrayList<Vaccinecontrol>();

        for (int i = 0; i < objList.size(); i++) {

            Varray.add(GetObject(i));
        }
        return Varray;
    }

    public String PrepareParameters() {

        String control = "";
        String id = "";
        String ret = "";

        id = (this.idVaccinecontrol != null) ? ("idVaccinecontrol=" + this.idVaccinecontrol) : "";
        control = (this.Control_idControl != null) ? ("Control_idControl=" + 
                this.Control_idControl.toString()) : "";

        if (!"".equals(id) || !"".equals(control)) {
            return "?" + id + "&" + control;
        }
        return ret;
    }

    public QVaccinecontrol(Integer idVaccine) throws IOException, JSONException {

        super();

        if (idVaccine != null) {

            this.idVaccinecontrol = idVaccine;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QVaccinecontrol(String vaccineName) throws IOException, JSONException {
        super();

        if (vaccineName != null) {

            this.dose = vaccineName;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QVaccinecontrol() throws IOException, JSONException {
        super();
    }

    public JSONArray getArray() {
        return array;
    }

    public void setArray(JSONArray array) {
        this.array = array;
    }

    public Integer getIdVaccine() {
        return idVaccinecontrol;
    }

    public void setIdVaccine(Integer idVaccine) {
        this.idVaccinecontrol = idVaccine;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public ArrayList<JSONObject> getObjList() {
        return objList;
    }

    public void setObjList(ArrayList<JSONObject> objList) {
        this.objList = objList;
    }

    public String getTable_name() {
        return table_name;
    }

    public void setTable_name(String table_name) {
        this.table_name = table_name;
    }

    public String getVaccineName() {
        return dose;
    }

    public void setVaccineName(String vaccineName) {
        this.dose = vaccineName;
    }
}
